#include "text_area.h"

#include "text_widget.h"

bool TextArea::isEmpty() const
{
    return (_text->toPlainText() == "");
}

void TextArea::resizeEvent(QResizeEvent *)
{
    _text->setGeometry(QRect(15, 3, width()-18, height()-6));
}

TextArea::TextArea(QWidget* parent, int x, int y, int width, int height, int id, int serial) :
		Area(parent, x, y, width, height, id, serial)
{
    _text = new TextWidget(this);
    _text->setObjectName(QString::fromUtf8("Text"));
    _text->setGeometry(QRect(15, 3, this->width()-18, this->height()-6));
    _text->setFrameShape(QFrame::NoFrame);

    QPalette palette1;
    QBrush brush1(QColor(255, 255, 255, 255));
    brush1.setStyle(Qt::SolidPattern);
    palette1.setBrush(QPalette::Active, QPalette::Base, brush1);
    palette1.setBrush(QPalette::Active, QPalette::Window, brush1);
    palette1.setBrush(QPalette::Inactive, QPalette::Base, brush1);
    palette1.setBrush(QPalette::Inactive, QPalette::Window, brush1);
    palette1.setBrush(QPalette::Disabled, QPalette::Base, brush1);
    palette1.setBrush(QPalette::Disabled, QPalette::Window, brush1);
    _text->setPalette(palette1);

    _text->setAutoFillBackground(true);

    //_text->setHtml(data);

    _text->setAlignment(Qt::AlignCenter);
    _text->setFocus();

    _font = QFont("Times", 18, QFont::Normal);
    setFont(_font);

}

void TextArea::setData(const QString & data)
{
    _text->setHtml(data);
}

QByteArray TextArea::contains() const
{
    QString data_ = _text->toHtml().toUtf8();

    QByteArray data = data_.toStdString().c_str();

    return ("<object type=text x="+
            QByteArray::number(this->x())+" y="+
            QByteArray::number(this->y())+" w="+
            QByteArray::number(this->width())+" h="+
            QByteArray::number(this->height())+" r="+
            QByteArray::number(_color.red())+" g="+
            QByteArray::number(_color.green())+" b="+
            QByteArray::number(_color.blue())+" serial="+
            QByteArray::number(_serial)+">"+data+"</object>\n");
}

TextArea::~TextArea()
{
    delete _text;
}

void TextArea::setFont(const QFont &font)
{
    _text->setFont(font);
    _font = _text->font();
}

void TextArea::setFontSize(int width)
{    
    _font.setPointSize(width);
    setFont(_font);
}

void TextArea::fontZoomIn()
{
    setFontSize(_font.pointSize()*1.2);
}

void TextArea::fontZoomOut()
{
    setFontSize(_font.pointSize()/1.2);
}
